package biz.bbtec.sbm.issuetracking.action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import biz.bbtec.sbm.issuetracking.bean.AttachmentBean;
import biz.bbtec.sbm.issuetracking.bean.NoteBean;
import biz.bbtec.sbm.issuetracking.bean.WorkBean;
import biz.bbtec.sbm.issuetracking.factory.NoteFactory;
import biz.bbtec.sbm.issuetracking.service.NoteService;

@SuppressWarnings("serial")
public class NoteAction extends ActionSupport implements SessionAware {
	private NoteBean note = new NoteBean();
	private List<NoteBean> notelist = new ArrayList<NoteBean>();
	private Map<String, Object> session;
	private WorkBean work;
	private String wptitle;
	private File image;
	private String imageFileName;
	private String imageFileContent;
	private int pageNow;
	private String taskname;
	public String getTaskname() {
		return taskname;
	}

	public void setTaskname(String taskname) {
		this.taskname = taskname;
	}

	public String getWptitle() {
		return wptitle;
	}

	public void setWptitle(String wptitle) {
		this.wptitle = wptitle;
	}

	public int getPageNow() {
		return pageNow;
	}

	public void setPageNow(int pageNow) {
		this.pageNow = pageNow;
	}

	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public String getImageFileContent() {
		return imageFileContent;
	}

	public void setImageFileContent(String imageFileContent) {
		this.imageFileContent = imageFileContent;
	}

	public WorkBean getWork() {
		return work;
	}

	public void setWork(WorkBean work) {
		this.work = work;
	}

	public List<NoteBean> getNotelist() {
		return notelist;
	}

	public void setNotelist(List<NoteBean> notelist) {
		this.notelist = notelist;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public NoteBean getNote() {
		return note;
	}

	public void setNote(NoteBean note) {
		this.note = note;
	}

	public String modifyNote() throws Exception {
		if (note.getTitle().trim().equalsIgnoreCase("")) {
			this.addActionError("留言名称不能为空！");
			return INPUT;
		}
		if (note.getText().trim().equalsIgnoreCase("")) {
			this.addActionError("留言内容不能为空！");
			return INPUT;
		}
		NoteService noteservice = NoteFactory.getInstance().creatNoteService();
		if (!noteservice.ModifyNote(note)) {
			this.addActionError("数据库操作失败！");
			return INPUT;
		}
		Map session = ActionContext.getContext().getSession();
		session.remove("note");
		return SUCCESS;
	}

	public String deleteNote() throws Exception {
		NoteService noteservice = NoteFactory.getInstance().creatNoteService();
		List<AttachmentBean> attlist = noteservice.attlist(note);

		if (attlist.size() != 0) {
			String path = ServletActionContext.getServletContext().getRealPath(
					"images");
			for (int i = 0; i < attlist.size(); i++) {
				File f = new File(path + "\\" + attlist.get(i).getTitle());
				if (f.exists()) {
					f.delete();
				}
			}
		}
		if (!noteservice.DeleteNote(note)) {
			this.addActionError("未知错误！");
			return INPUT;
		} else {
			this.addActionError("删除成功！");
			return SUCCESS;
		}
	}

	public String addNote() throws Exception {
		if (image != null) {
			Random rnd = new Random();
			int p = rnd.nextInt(20);
			String num = Integer.toString(p);

			String path = ServletActionContext.getServletContext().getRealPath(
					"/images");

			imageFileName = num + "." + imageFileName;

			File file = new File(path + "//" + imageFileName);
			FileUtils.copyFile(image, file);
		} else {
			imageFileName = null;
		}

		if (note.getTitle().trim().equalsIgnoreCase("")) {
			this.addActionError("留言名称不能为空！");
			return INPUT;
		}
		if (note.getText().trim().equalsIgnoreCase("")) {
			this.addActionError("留言内容不能为空！");
			return INPUT;
		}
		NoteService noteservice = NoteFactory.getInstance().creatNoteService();
		if (!noteservice.AddNote(note, imageFileName)) {
			this.addActionError("数据库操作失败！");
			return INPUT;
		} else {
			this.addActionError("添加成功！");
			return SUCCESS;
		}
	}

	public String showallNote() throws Exception {
		if (note.getWpid() != 0) {
			work = new WorkBean();
			work.setWpid(note.getWpid());
		}
		NoteService noteservice = NoteFactory.getInstance().creatNoteService();
		notelist = noteservice.ShowAllNote(work, getPageNow(), 15);
		if (getPageNow() == 0) {
			int total = noteservice.CountAllNote(work);
			int page = 0;
			do {
				total = total - 15;
				page++;
			} while (total > 0);
			this.session.put("notetotalpage", page);
		}
		wptitle = noteservice.showwptitle(work);
		if (notelist.size() == 0) {
			this.addActionError("没有留言，请添加！");
			this.session.put("wpid", work.getWpid());
			this.session.put("addnotewpid", work.getWpid());
			return INPUT;
		} else {
			taskname = noteservice.showTaskName(notelist.get(0).getTaskid());
			int notewpid = notelist.get(0).getWpid();
			this.session.put("wpid", notewpid);
			this.session.put("addnotewpid", notewpid);
			return SUCCESS;
		}
	}

	public String addshowallNote() throws Exception {
		Map session = ActionContext.getContext().getSession();
		if (session.get("addnotewpid") != null) {
			int wpid = (Integer) session.get("addnotewpid");
			work = new WorkBean();
			work.setWpid(wpid);
			session.remove("addnotewpid");
		}
		NoteService noteservice = NoteFactory.getInstance().creatNoteService();
		notelist = noteservice.ShowAllNote(work, getPageNow(), 15);
		if (getPageNow() == 0) {
			int total = noteservice.CountAllNote(work);
			int page = 0;
			do {
				total = total - 15;
				page++;
			} while (total > 0);
			this.session.put("notetotalpage", page);
		}
		wptitle = noteservice.showwptitle(work);
		if (notelist.size() == 0) {
			this.addActionError("没有留言，请添加！");
			this.session.put("wpid", work.getWpid());
			this.session.put("addnotewpid", work.getWpid());
			return INPUT;
		} else {
			int notewpid = notelist.get(0).getWpid();
			this.session.put("wpid", notewpid);
			this.session.put("addnotewpid", notewpid);
			return SUCCESS;
		}
	}

	public String showoneNote() throws Exception {
		work = new WorkBean();
		work.setWpid(note.getWpid());
		NoteService noteservice = NoteFactory.getInstance().creatNoteService();
		note = noteservice.ShowOneNote(note);
		if (note == null) {
			this.addActionError("出错啦！");
			return INPUT;
		} else {
			this.session.put("note", note);
			return SUCCESS;
		}
	}
}
